package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.mapper.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Package;
import com.woniu.service.IFrontDoctorService;
import com.woniu.vo.QueryServiceDetailVo;
import com.woniu.vo.ServiceSubmitVo;
import com.woniu.vo.SignVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class FrontDoctorServiceImpl implements IFrontDoctorService {

    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private SignMapper signMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserDetailMapper userDetailMapper;
    @Resource
    private UserSignStatusMapper userSignStatusMapper;
    @Resource
    private UserLabelMapper userLabelMapper;
    @Resource
    private LabelMapper labelMapper;
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private PackageMapper packageMapper;
    @Resource
    private  StringRedisTemplate stringRedisTemplate;
    @Resource
    private ServicesMapper  servicesMapper;
    @Resource
    private PackageItemContactMapper packageItemMapper;
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private FamilyMapper familyMapper;



    /**
     * 根据医生的id查询所有签约
     * @param doctorId
     * @return
     */
    public List<SignVo> querySignByDoctorId(Long doctorId) {
        QueryWrapper<Sign> signQueryWrapper = new QueryWrapper<>();
        //构造条件 status 是待审核
        signQueryWrapper.eq("doctor_id",doctorId);
        List<Sign> signs = signMapper.selectList(signQueryWrapper);
        List<SignVo> signVoList=new ArrayList<>();
        for (Sign sign : signs) {
            SignVo signVo = signToSignVo(sign, new SignVo());
            signVoList.add(signVo);
        }
        return signVoList;
    }

    /**
     * 审核签约通过
     * @param signVo
     * @return
     */
    public Boolean audit(SignVo signVo) {
        //根据id查询该签约
        Long id = signVo.getId();
        Sign sign = signMapper.selectById(id);
        //修改成为待支付
        sign.setStatus("1");
        //未支付，等待支付
        sign.setSignStatusId(6L);
        int updateById = signMapper.updateById(sign);
        //清除审核的redis
        stringRedisTemplate.delete("audit"+id);
        //设置定时10分，如果长时间间未支付，设置为过期
        stringRedisTemplate.opsForValue().set("payTime"+id, "已支付", 60 * 10, TimeUnit.SECONDS);
        if (updateById<0){
            return false;
        }
        return true;
    }

    /**
     * 服务 项目
     * @param queryServiceDetailVo
     * @return
     */
    public Boolean doService(QueryServiceDetailVo queryServiceDetailVo) {
        Long id = queryServiceDetailVo.getId();
        //根据id查询服务
        Services services = servicesMapper.selectById(id);
        //服务已经完成4
        services.setServiceStatusId(4L);
//        修改
        int updateById = servicesMapper.updateById(services);
        if (updateById<0){
            return false;
        }
        return true;
    }

    /**
     * 根据医生id查询所有的服务
     * @param doctorId
     * @return
     */
    public List<QueryServiceDetailVo> queryServicesByDoctorId(Long doctorId) {

        QueryWrapper<Services> servicesQueryWrapper = new QueryWrapper<>();
        servicesQueryWrapper.eq("doctor_id",doctorId);
        List<Services> services = servicesMapper.selectList(servicesQueryWrapper);
        List<QueryServiceDetailVo> serviceDetailVoList=new ArrayList<>();
        for (Services service : services) {
            QueryServiceDetailVo queryServiceDetailVo = serviceToQueryServiceDetailVo(service, new QueryServiceDetailVo());
            serviceDetailVoList.add(queryServiceDetailVo);
        }
        return serviceDetailVoList;
    }

    /**
     * 审核
     * @param queryServiceDetailVo
     * @return
     */
    public Boolean auditService(QueryServiceDetailVo queryServiceDetailVo) {
        Long id = queryServiceDetailVo.getId();
        //根据id查询服务
        Services services = servicesMapper.selectById(id);
        //审核通过变成待服务2
        services.setServiceStatusId(2L);

//        修改
        int updateById = servicesMapper.updateById(services);
        if (updateById<0){
            return false;
        }
        return true;
    }

    /**
     * 驳回服务
     * @param queryServiceDetailVo
     * @return
     */
    public Boolean refuseService(QueryServiceDetailVo queryServiceDetailVo) {

        Long id = queryServiceDetailVo.getId();
        //根据id查询服务
        Services services = servicesMapper.selectById(id);
        //驳回3
        services.setServiceStatusId(3L);
//        修改
        int updateById = servicesMapper.updateById(services);
        if (updateById<0){
            return false;
        }
        return true;
    }

    /**
     * 驳回签约
     * @param signVo
     * @return
     */
    public Boolean refuseSign(SignVo signVo) {
        Long id = signVo.getId();
        //根据id查询服务
        Sign sign = signMapper.selectById(id);
        //驳回3
        sign.setStatus("3");
        sign.setSignStatusId(3L);
       //        修改
        int updateById = signMapper.updateById(sign);
        //设置redis
        stringRedisTemplate.opsForValue().set("refuse:"+id,"驳回");
        if (updateById<0){
            return false;
        }
        return true;

    }


    /**
     * 转换
     * @param sign
     * @param signVo
     * @return
     */
    public SignVo signToSignVo(Sign sign,SignVo signVo){
        BeanUtils.copyProperties(sign,signVo);
        signVo.setApplyTime(sign.getApplyTime());
        //user
        User user = userMapper.selectById(sign.getUserId());

        BeanUtils.copyProperties(user,signVo);
        signVo.setId(sign.getId());
        //用户详情
        QueryWrapper<UserDetail> userDetailQueryWrapper = new QueryWrapper<>();
        userDetailQueryWrapper.eq("user_id",user.getId());
        //现居住地址
        UserDetail userDetail = userDetailMapper.selectOne(userDetailQueryWrapper);
        String addressNow = userDetail.getAddressNow();
        signVo.setAddressNow(addressNow);
        //签约状态
        Long signStatusId = sign.getSignStatusId();
        UserSignStatus userSignStatus = userSignStatusMapper.selectById(signStatusId);
        signVo.setSignStatus(userSignStatus.getName());
        //标签
        QueryWrapper<UserLabel> userLabelWrapper = new QueryWrapper<>();
        userLabelWrapper.eq("user_id",user.getId());
        List<UserLabel> userLabels = userLabelMapper.selectList(userLabelWrapper);
        List<Label> labelList=new ArrayList<>();
        for (UserLabel userLabel : userLabels) {
            Long labelId = userLabel.getLabelId();
            Label label = labelMapper.selectById(labelId);
            labelList.add(label);
        }
        signVo.setLabelList(labelList);
        //机构
        Organization organization = organizationMapper.selectById(sign.getOrganizationId());
        signVo.setOrganizationName(organization.getName());
        //团队
        Team team = teamMapper.selectById(sign.getTeamId());
        signVo.setTeamName(team.getName());
        //服务包
        Package aPackage = packageMapper.selectById(sign.getPackageId());
        signVo.setPackageName(aPackage.getName());
        //花费
        BigDecimal price = aPackage.getPrice();
        signVo.setCost(price);
        //医生
        Doctor doctor = doctorMapper.selectById(sign.getDoctorId());
        signVo.setDoctorName(doctor.getName());
        return signVo;
    }

    /**
     * services转换 queryServiceDetailVo
     * @param services
     * @param queryServiceDetailVo
     * @return
     */
    public QueryServiceDetailVo serviceToQueryServiceDetailVo(Services services,QueryServiceDetailVo queryServiceDetailVo){
         //复制
        BeanUtils.copyProperties(services,queryServiceDetailVo);
        //医生
        Doctor doctor = doctorMapper.selectById(services.getDoctorId());
        queryServiceDetailVo.setDoctorName(doctor.getName());
        //用户
        User user = userMapper.selectById(services.getUserId());
        BeanUtils.copyProperties(user,queryServiceDetailVo);

        //由于id被覆盖，重新设置id
        queryServiceDetailVo.setId(services.getId());
        queryServiceDetailVo.setUserId(user.getId());
        //团队
        Long teamId = user.getTeamId();
        Team team = teamMapper.selectById(teamId);
        queryServiceDetailVo.setTeamName(team.getName());
        //服务包
        Long packageId = user.getPackageId();
        Package aPackage = packageMapper.selectById(packageId);
        queryServiceDetailVo.setPageName(aPackage.getName());
        //服务项目
        Long itemId = services.getItemId();
        Item item = itemMapper.selectById(itemId);
        queryServiceDetailVo.setItemName(item.getName());
        //标签
        QueryWrapper<UserLabel> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("user_id",user.getId());
        List<UserLabel> userLabels = userLabelMapper.selectList(wrapper2);
        ArrayList<Label> labelArrayList = new ArrayList<>();
        for (UserLabel userLabel : userLabels) {
            Long labelId = userLabel.getLabelId();
            Label label = labelMapper.selectById(labelId);
            labelArrayList.add(label);
        }
        queryServiceDetailVo.setLabelList(labelArrayList);
        //家庭成员
        QueryWrapper<Family> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("user_id",user.getId());
        List<Family> families = familyMapper.selectList(wrapper3);
        queryServiceDetailVo.setFamilyList(families);
        return queryServiceDetailVo;
    }
}
